package com.egghiro.framework.j2se.game.controller.command;

import java.util.Iterator;

import com.egghiro.framework.j2se.util.SArrayList;

/**
 * 
 * @designPattern Command Pattern
 * @characterInPattern ConcreteCommand
 * @usedPattern Composite Pattern
 * @characterInPattern Composite
 * @date 2011-5-29
 * @version 0.1
 */
public class MacroCommand implements Command {
	private SArrayList<Command> commands = new SArrayList<Command>();

	/**
	 * 
	 */
	public void execute() {
//		Iterator<Command> it = commands.iterator();
//		while (it.hasNext()) {
//			it.next().execute();
//		}
	    for(Command command : commands){
	        command.execute();
	    }
	}

	public void append(Command cmd) {
		if (cmd != this) { //TODO 親を渡されたらどうしよう。。。
			commands.add(cmd);
		}
	}

	public void undo() {
		if (!commands.isEmpty()) {
			commands.remove(commands.size() - 1);
		}
	}

	public void clear() {
		commands.clear();
	}

}
